const express = require('express');
const router = express.Router();
const NavModel = require('../../model/nav.js')
router.get('/',async (req,res)=>{
    let navlist = await NavModel.find({})
    if(navlist.length>0){
        res.render('admin/nav/index.html',{list:navlist})
    }else{
        res.render('admin/status',{
            redirectUrl:'/admin/nav/add',
            title:'没有数据',
            message:'添加页面'
        })
    }
    
})
router.get('/add',(req,res)=>{
    res.render('admin/nav/add.html',{})
})
router.post('/doadd',(req,res)=>{
    let nav = new NavModel(Object.assign(req.body,{add_time:Date.now()}));
    nav.save()
    res.render('admin/status',{
        redirectUrl:'/admin/nav',
        title:'添加成功',
        message:'nav主页面'
    })
})
router.get('/edit',async (req,res)=>{
    let id = req.query.id;
    // NavModel.updateOne({_id:id},Object.assign(req.body,{add_time:Date.now()}))
    let result = await NavModel.find({_id:id})

       res.render('admin/nav/edit',result[0])

})
router.post('/doEdit',async (req,res)=>{
    let id = req.body.id;
    await  NavModel.updateOne({_id:id},Object.assign(req.body,{add_time:Date.now()}))
    res.redirect('/admin/nav')
})
router.get('/delete',async (req,res)=>{
    let id = req.query.id;
    await NavModel.deleteOne({_id:id})
    res.redirect('/admin/nav')
})
module.exports = router;